import Http from '@/libs/http'
import md5 from '@/vendor/md5'
import utils from "@/libs/utils";

export const http = new Http()
export const loginHttp = new Http(true)

export const commonApi = {
  test() {
    return loginHttp.get('xx')
  },
  updateKemu(kemu) {
    return http.post('user/kemu', {
      data: {
        kemu
      }
    })
  },
  autoLogin(provider, authResult) {
    return loginHttp.post('user/auto/login', {
      data: {
        provider, authResult,
      }
    }).then(res => {
      http.init()
      return res
    })
  }, decryptPhone(provider, authResult, iv, encryptedData) {
    return http.post('user/decrypt/phone', {
      data: {
        provider, authResult, iv, encryptedData
      }
    })
  }, refreshUser() {
    return http.get('user/info')
  }, userData() {
    return http.get('user/data')
  }, /**
   * 检查该账号是否已经存在
   * @returns {*}
   */
  checkUserIsExist(data) {
    return http.post('checkUserIsExist', {data})
  }, getUploadToken() {
    return http.get('qiniu/upload/token')
  }, thirdLogin(provider, authResult, code, user, useNew, inviteUserId, systemInfo) {
    let url = 'user/login'
    return http.post(url, {
      data: {
        provider, authResult, code, user, useNew, inviteUserId
      }
    })
  },
  logout() {
    return http.get('user/logout')
  },
  checkVersion() {
    return http.get('check/app/version')
  },
  checkVersionExpired() {
    return http.get('check/app/version/expired')
  },
  kemu1KaobuguoExam() {
    return http.get('user/kemu1/KaobuguoExam')
  },
  inviteVips() {
    return http.get('user/invite_vips')
  },
  activeVIP(license) {
    return http.post('user/active_vip', {
      data: {
        license
      }
    })
  },
  unregister() {
    return http.post('user/unregister')
  },
  updateName(name) {
    return http.post('user/name', {
      data: {
        name
      }
    })
  },
  updateCarType(carType) {
    return http.post('user/carType', {
      data: {
        carType
      }
    })
  }, updateAvatar(avatar) {
    return http.post('user/avatar', {
      data: {
        avatar
      }
    })
  }, getCaptcha(phone) {
    const {timestamp, token} = this.getCaptchaToken(phone);
    return http.get('getCaptcha', {
      data: {
        phone, timestamp, token
      }
    })
  }, getCaptchaToken(phone) {
    const timestamp = Date.now();
    const token = md5(md5(phone + timestamp + 'jiakao') + timestamp + 'wsdk');
    return {timestamp, token}
  },
  checkBound(provider, authResult) {
    return http.post('user/checkBound', {
      data: {
        provider, authResult
      }
    })
  },
  bindMerge(provider, authResult) {
    return http.post('user/bindMerge', {
      data: {
        provider, authResult
      }
    })
  },
  getInviteWXShortLink() {
    return http.post('user/invite_wx_shortlink')
  }, optionYuyin(option) {
    return http.get('yuyin/option', {
      data: {
        option,
        per: 5118
      }
    })
  }, questionYuyin(id, option, answer, jx, vip) {
    let url = 'yuyin/questions/' + id + '?'
    if (option) {
      url += 'option=1&'
    }
    if (answer) {
      url += 'answer=1&'
    }
    if (jx) {
      url += 'jx=1&'
    }
    if (vip) {
      url += 'vip=1&'
    }
    return http.get(url)
  }, getAvatar(userid) {
    return http.get('user/avatar?userId=' + userid)
  }, getVip(inviter) {
    return http.get('getVip?inviteUserId=' + inviter)
  }, getSharePosterImageUrl(userId) {
    return http.get('appIcon/createUserQrCode/' + userId, {
      returnRequest: true
    }).url
  },

  citys() {
    return http.get('citys')
  }, questions() {
    return http.static('http://down.qwdata.vip/question_2_e.txt', {
      key: 'questions'
    })
  }, tags() {
    return http.static('http://down.qwdata.vip/tag_1680741069202.txt', {
      key: 'tags'
    })
  }, sign() {
    return new Promise(resolve => {
      uni.request({
        url: 'http://down.qwdata.vip/sign.txt', success: (res) => {
          resolve(res)
        }
      })
    })
  },
  uploadScan(str) {
    return http.post('upload/scan', {
      data: {
        str
      }
    })
  },
  uploadEvents(events) {
    return http.post('events', {
      data: {
        events
      }
    })
  },
  uploadCard(realName, cardNo) {
    return http.post('user/card_id', {
      data: {
        cardNo, realName
      }
    })
  },
  locationToCity(lat, lang) {
    return http.post('map/city', {
      data: {
        location: lat + ',' + lang
      }
    })
  },
  uploadAD(type) {
    return http.post('ad/count', {
      data: {
        type
      }
    })
  },
  uploadInstalledApps(list) {
    return http.post('installed/apps', {
      data: {
        list
      }
    })
  },
  startTiyan() {
    return http.post('user/tiyan', {
      data: {
      }
    })
  },
  changeCity(code) {
    return http.post('user/city', {
      data: {
        code
      }
    })
  },
}
export const yuyinApi = {
  token() {
    return http.get('yuyin/token')
  }, yuyin(tex, token, per = 1, spd = 5) {
    return new Promise(resolve => {
      uni.request({
        url: 'https://tsn.baidu.com/text2audio', header: {
          contentType: 'application/x-www-form-urlencoded',
        }, data: {
          tex, cuid: '12131', ctp: '1', lan: 'zh', tok: token, per, spd,
        }, success: (res) => {
          resolve(res.data)
        }
      })
    })
  }
}

export const qrCodeApi = {
  getQr1() {
    return http.get('appIcon/qrCode1')
  }, getQr2() {
    return http.get('appIcon/qrCode2')
  }
}

export const douyinOpenApi = {
  accessToken(code) {
    return http.get('douyinOpenAccessToken?code=' + code)
  }
}

export const productApi = {
  info(id) {
    return http.get('products/' + id)
  },
  list(ids) {
    return http.get('products?ids=' + ids)
  }
}
export const courseApi = {
  info(id) {
    return http.get('courses/' + id)
  },
  content(contentId) {
    return http.get('courses/contents/' + contentId)
  },
  contentsByTag(id, tagId) {
    return http.get('courses/' + id + '/contentsByTag?tagId=' + tagId)
  },
  contentsByQuestion(id, questionId) {
    return http.get('courses/' + id + '/contentsByQuestion?questionId=' + questionId)
  },
  postCourseToMail(courseId, mail) {
    return http.post('courses/' + courseId + '/mail', {
      data: {
        mail
      }
    })
  }
}

export const orderApi = {
  payOrder(channelCode, orderId) {
    let url = this._channelPayPath(channelCode) + '?id=' + orderId
    return http.get(url)
  }, _channelPayPath(channelCode) {
    let path;
    // #ifdef APP-PLUS
    path = channelCode + '/app'
    // #endif
    // #ifdef MP
    path = channelCode + '/xcx'
    // #endif

    if (channelCode === 'test') {
      path = channelCode
    }

    if (!channelCode || !path) {
      throw new Error('该环境不支持支付')
    }

    return 'pay/' + path
  },

  addPercent(id) {
    return http.post('orders/' + id + '/percent')
  },

  buy(channelCode, productId) {
    let url = this._channelPayPath(channelCode) + '?productId=' + productId
    return http.get(url)
  },

  // #ifdef APP-PLUS
  appResultUpload(receiptData, order_id) {
    return http.post('pay/applepay/app/result', {
      data: {
        receiptData, order_id
      }
    })
  }, // #endif
  pollSuccess(id) {
    return http.get('order/' + id + '/success')
  },

  info(id) {
    return http.get('orders/' + id)
  },

  list(type, status) {
    const data = {
      type
    }
    if (status != null) {
      data.status = status
    }
    return http.get('orders', {
      data
    })
  }
}

export const msgApi = {
  poll() {
    return http.get('msgs/new', {log: false})
  }, report() {
    return http.get('msgs/report')
  }
}

export const errorCorrectionApi = {
  create(questionId, type, content, phone, images) {
    return http.post('error_corrections', {
      data: {
        questionId, type, content, phone, images
      }
    })
  }
}
export const reportApi = {
  create(data) {
    return http.post('reports', {
      data
    })
  }
}

export const reportRecordApi = {
  create(status) {
    return http.post('user/report_records', {
      data: {
        status
      }
    })
  }
}

export const settleInApi = {
  create(type, contact, phone, wechat) {
    return http.post('settle_ins', {
      data: {
        type, contact, phone, wechat
      }
    })
  }
}

export const searchApi = {
  searchRelate(kws) {
    return http.post('search/relate', {
      data: {
        kws
      }
    })
  }, hotSearches() {
    return http.get('search/hot')
  }, list(kws = null) {
    let url = 'questions/search'
    if (kws) {
      url += '?kws=' + kws
    }
    return http.get(url)
  },
}

export const shorthandApi = {
  list() {
    return http.get('shorthands')
  }, items(id) {
    return http.get('shorthands/' + id + '/items')
  }
}

export const schoolApi = {
  info(id) {
    return http.get('schools/' + id).then(res => {
      res.data.data = utils.decrypt(res.data.data)
      return res
    })
  }, list(index = 0, lng, lat, type, searchKWs) {
    let url = 'schools?index=' + index + '&lng=' + lng + '&lat=' + lat + '&type=' + type
    if (searchKWs) {
      url += '&searchKWs=' + searchKWs
    }
    return http.get(url).then(res => {
      res.data.data = utils.decrypt(res.data.data, index)
      return res
    })
  }, createRegistration(type, schoolId, phone, cityCode, baoming) {
    return http.post('schools/registrations', {
      data: {
        type, schoolId, phone, cityCode, baoming
      }
    })
  }
}

export const coachApi = {
  info(id) {
    return http.get('schools/coaches/' + id)
  }
}

export const questionApi = {
  info(id) {
    // id = '14632005'
    // id = '14632004'
    // id = '14632001'
    // id = '14631977'
    return http.get('questions/' + id + '/info').then(res => {
      res.data.data = utils.decrypt(res.data.data, id)
      // console.log(res.data.data)
      return res
    })
  }, locationList(kemu = 1) {
    return http.get('location/questions?kemu=' + kemu)
  }, mijuanList(kemu) {
    return http.get('questions/categories?kemu=' + kemu)
  }, list(type, kemu = null, kws = null) {
    let url = 'questions?type=' + type
    if (kws) {
      url += '&kws=' + kws
    }

    if (kemu) {
      url += '&kemu=' + kemu
    }
    return http.get(url)
  }, tags(kemu) {
    return http.get('tags?kemu=' + kemu)
  },
  tag(type) {
    return http.get('tag?tagId=' + type)
  },
  tagCount() {
    return http.get('tags/count')
  }, category(categoryId) {
    return http.get('categories/' + categoryId + '/sign_icons')
  }, icon(categoryId, id) {
    return http.get('categories/' + categoryId + '/sign_icons/' + id)
  }, categories() {
    return http.get('categories')
  }, collect(questionId, kemu) {
    return http.post('collect', {
      data: {
        questionId, kemu
      }
    })
  },

  collects(kemu, today) {
    let url = 'collects?kemu=' + kemu
    if (today) {
      url += '&today=' + today
    }
    return http.get(url)
  },

  wrongs(kemu, today) {
    let url = 'wrongs?kemu=' + kemu
    if (today) {
      url += '&today=' + today
    }
    return http.get(url)
  },

  unCollect(questionId, kemu) {
    return http.post('unCollect', {
      data: {
        questionId, kemu
      }
    })
  },

  clearAnswer(type) {
    return http.post('questions/answer/clear', {
      data: {
        type,
      }
    })
  }, answer(type, questionId, result, status, kemu) {
    return http.post('questions/answer2', {
      data: {
        type, questionId, result, status, kemu,
      }
    })
  }, preExam(type) {
    return http.get('questions/count?type=' + type)
  }, deleteWrong(questionId) {
    return http.post('deleteWrong', {
      data: {
        questionId
      }
    })
  }
}
export const examResultApi = {
  list(key, kemu) {
    let url = 'exam_results?key=' + key + '&kemu=' + kemu
    return http.get(url)
  }, count() {
    return http.get('exam_results/count')
  }, save(key, score, status, time, kemu, zhengque, cuowu) {
    return http.post('exam_results', {
      data: {
        key, score, status, kemu, time, zhengque, cuowu
      }
    })
  }
}

export const douyinAPi = {
  fansGroupList(code) {
    return http.get('douyin/fans_group/list', {
      data: {
        code
      }
    })
  },
}
